package huixue.cn.com.rxapp.stock;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;

import huixue.cn.com.rxapp.stock.util.GridUtils;
import huixue.cn.com.rxapp.widget.data.EntryData;

/**
 * Created by Administrator on 2018/8/19.
 */

public class Renderer {

    protected final Paint mPaint;
    protected final Paint mLabelPaint;
    private Paint.FontMetrics fontMetrics = new Paint.FontMetrics();
    private float defaultTextSize;

    protected RectF  candleRect = new RectF();
    protected RectF  barRect = new RectF();
    private final float candleBarRatio = 0.8f;// candleRect.height / barRect.height = 4
    protected EntryData data;
    protected int visibleCount = 100;

    /**
     * matrix to map the values to the screen pixels
     */
    protected Matrix mMatrixValue = new Matrix();

    /**
     * matrix to map chart scaled pixels
     */
    protected Matrix mMatrixTouch = new Matrix();

    /**
     * matrix to map the chart offset
     */
    protected Matrix mMatrixOffset = new Matrix();

    /**
     * matrix to map the volume value
     */
    protected Matrix mMatrixBar = new Matrix();

    protected int visibleXMin, visibleXMax;
    protected float maxTouchOffset, minTouchOffset;

    protected float[] matrixValues = new float[9];
    private boolean isOnBorder = true;

    public Renderer() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(1);



        mLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLabelPaint.setColor(Color.BLACK);
    }

    public void setContentRect(RectF contentRect) {

        float barTop = contentRect.bottom -
                (1 - candleBarRatio) * (contentRect.height() - contentRect.top);

        float candleBottom = barTop - contentRect.top;

        this.candleRect.set(contentRect.left, contentRect.top, contentRect.right, candleBottom);

        this.barRect.set(contentRect.left, barTop, contentRect.right, contentRect.bottom);

        defaultTextSize = contentRect.top * 3 / 4;
    }

    public void render(Canvas canvas) {

        renderGrid(canvas);
    }

    protected void renderGrid(Canvas canvas) {
        // CALC
        calc();
        GridUtils.drawGrid(canvas,candleRect);
        //canvas.drawLine(candleRect.left, candleRect.height() / 2 + candleRect.top, candleRect.right, candleRect.height() / 2 + candleRect.top, mGridPaint);
    }

    /**
     * Calculate the current range of x and y.
     */
    protected void calc() {
        float[] pixels = new float[]{candleRect.right, 0};


    }


    public void setData(EntryData data) {
        this.data = data;

        prepareMatrixTouch(visibleCount);
        prepareMatrixValue(data.mYMax - data.mYMin, data.mYMin);
        prepareMatrixOffset(candleRect.left, candleRect.top);
    }

    public void prepareMatrixTouch(float visibleCount) {
        float scaleX = data.entries.size() / visibleCount;
        float scaleY = 1;

        mMatrixTouch.reset();
        mMatrixTouch.postScale(scaleX, scaleY);

        Log.e("ChannelIndex","mMatrixTouch=="+mMatrixTouch.toShortString());
        //resetScrollRange(scaleX);

    }

    //--------------------------------------------------------------------------------
    public void prepareMatrixValue(float deltaY, float yMin) {
        // increase the y range for good looking.
        deltaY = deltaY * 12 / 10;
        yMin = yMin * 9 / 10;

        float scaleX = candleRect.width() / data.entries.size();
        float scaleY = candleRect.height() / deltaY;

        mMatrixValue.reset();

        mMatrixValue.postTranslate(0, -yMin);
        // the negative scale factor is used to draw x axis from right to left,y from down to up
        mMatrixValue.postScale(-scaleX, -scaleY);
        mMatrixValue.postTranslate(candleRect.width(), candleRect.height());
    }

    public void prepareMatrixOffset(float offsetX, float offsetY) {
        mMatrixOffset.reset();
        mMatrixOffset.postTranslate(offsetX, offsetY);
    }
}
